/*
 * EcmaScriptLexicalException.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.exceptions;

import org.scriptme.ecmascript.interpreter.EvaluationSource;
import org.scriptme.ecmascript.parser.EcmaScriptConstants;
import org.scriptme.ecmascript.parser.TokenMgrError;

/**
 * Exception used to signal lexical error during parsing.
 */
public class EcmaScriptLexicalException extends EcmaScriptException implements
		EcmaScriptConstants {

	/** The token mgr error. */
	private TokenMgrError tokenMgrError;

	/** The evaluation source. */
	private EvaluationSource evaluationSource;

	/**
	 * Create a new lexical exception.
	 * 
	 * @param e
	 *            The error from the token manager
	 * @param s
	 *            The evaluation source location of the error
	 */
	public EcmaScriptLexicalException(TokenMgrError e, EvaluationSource s) {
		super("Lexical error");
		tokenMgrError = e;
		evaluationSource = s;
	}

	/**
	 * Get the line number of the error if possible.
	 * 
	 * @return the line number
	 */
	public int getLineNumber() {

		if (evaluationSource != null) {
			return evaluationSource.getLineNumber();
		} else {
			return -1;
		}
	}

	/**
	 * Return the text of the token error and the location.
	 * 
	 * @return the message
	 */
	public String getMessage() {
		String retval = tokenMgrError.getMessage();
		retval += eol + evaluationSource;
		return retval;
	}

	/**
	 * Return true in case of unclosed comment, as in this case the statement is
	 * not complete and the user may be prompted to complete the statement.
	 * 
	 * @return true, if checks if is incomplete
	 */
	public boolean isIncomplete() {
		String s = tokenMgrError.getMessage();
		return s.indexOf(".  Probably unclosed comment.") != -1;
		// See TokenMgrError
	}
}
